﻿using System.Collections.Generic;
using System.Linq;
using DAL;
using DataContext;
using Entity;
using Library;
using Utils;

namespace BLL
{
    public class CtrFunction
    {
        /// <summary>
        /// Thêm mới một Function
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parrentID"></param>
        /// <param name="url"> </param>
        /// <param name="status"></param>
        public int AddFunction(string name, int parrentID, string url, int status)
        {
            name = HtmlUtility.HtmlEncode(name);
            url = HtmlUtility.HtmlEncode(url);
            return DB.AdminInstance.uspFunctionInsert(name, parrentID, url, status,AdminUtils.ServicesID);
        }

        /// <summary>
        /// Cập nhật thông tin cho Function
        /// </summary>
        /// <param name="functionID"></param>
        /// <param name="name"></param>
        /// <param name="parrentID"></param>
        /// <param name="url"> </param>
        /// <param name="order"></param>
        /// <param name="status"></param>
        public int UpdateFunction(int functionID, string name, int parrentID, string url, int order, int status)
        {
            name = HtmlUtility.HtmlEncode(name);
            url = HtmlUtility.HtmlEncode(url);
            return DB.AdminInstance.uspFunctionUpdate(functionID, name, parrentID, url, order, status, AdminUtils.ServicesID);
        }

        /// <summary>
        /// Xóa một Function
        /// </summary>
        /// <param name="functionID"></param>
        /// <returns></returns>
        public int DeleteFunction(int functionID)
        {
            return DB.AdminInstance.uspFunctionDelete(functionID);
        }

        /// <summary>
        /// Lấy danh sách Function theo danh mục cha
        /// </summary>
        /// <param name="parrentID"> </param>
        /// <param name="status">-1: Tất cả</param>
        /// <returns></returns>
        public ClassExtend<int, uspFunctionGetChildResult> GetChild(int parrentID, int status)
        {
            var objReturn = new ClassExtend<int, uspFunctionGetChildResult>
                                {
                                    Items =
                                        DB.AdminInstance.uspFunctionGetChild(AdminUtils.ServicesID,
                                                                                  parrentID, status).ToList()
                                };
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }

        /// <summary>
        /// Lấy danh sách Function theo danh mục cha cho chức năng quản lý Log
        /// </summary>
        /// <param name="parrentID"> </param>
        /// <param name="status">-1: Tất cả</param>
        /// <returns></returns>
        public ClassExtend<int, uspFunctionGetChildForLogResult> GetChildForLog(int parrentID, int status)
        {
            var objReturn = new ClassExtend<int, uspFunctionGetChildForLogResult>
            {
                Items =
                    DB.AdminInstance.uspFunctionGetChildForLog(AdminUtils.ServicesID, parrentID, status).ToList()
            };
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }

        /// <summary>
        /// Lấy thông tin chi tiết một Function
        /// </summary>
        /// <param name="functionID"></param>
        /// <returns></returns>
        public uspFunctionGetInfoResult GetInfoFunction(int functionID)
        {
            return DB.AdminInstance.uspFunctionGetInfo(functionID).FirstOrDefault();
        }

        /// <summary>
        /// Lấy danh sách Function theo cap
        /// </summary>
        /// <returns></returns>
        public ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildResult>> GetListFunction()
        {
            var objReturn = new ClassExtend<int, ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildResult>>();

            var listFunction = GetChild(-1, -1);
            foreach (var item in listFunction.Items)
            {
                var listFuncChild = new ClassExtend<uspFunctionGetChildResult, uspFunctionGetChildResult> {Info = item};
                var lstChild = GetChild(item.Id, -1).Items;
                listFuncChild.Items = lstChild;
                listFuncChild.TotalRecord = lstChild.Count;
                objReturn.Items.Add(listFuncChild);
            }
            objReturn.TotalRecord = listFunction.TotalRecord;
            return objReturn;
        }

        /// <summary>
        /// Lấy danh sách Function theo cap
        /// </summary>
        /// <returns></returns>
        public ClassExtend<int, ClassExtend<uspFunctionGetChildForLogResult, uspFunctionGetChildForLogResult>> GetListFunctionForLog()
        {
            var objReturn = new ClassExtend<int, ClassExtend<uspFunctionGetChildForLogResult, uspFunctionGetChildForLogResult>>();

            var listFunction = GetChildForLog(-1, -1);
            foreach (var item in listFunction.Items)
            {
                var listFuncChild = new ClassExtend<uspFunctionGetChildForLogResult, uspFunctionGetChildForLogResult> { Info = item };
                var lstChild = GetChildForLog(item.Id, -1).Items;
                listFuncChild.Items = lstChild;
                listFuncChild.TotalRecord = lstChild.Count;
                objReturn.Items.Add(listFuncChild);
            }
            objReturn.TotalRecord = listFunction.TotalRecord;
            return objReturn;
        }

        public List<uspFunctionGetListByAdminIdResult> GetListFunctionByAdminID(int adminID, int parrentID)
        {
            return DB.AdminInstance.uspFunctionGetListByAdminId(AdminUtils.ServicesID, adminID, parrentID).ToList();
        }

        public List<uspFunctionGetListByAdminRootResult> GetListFunctionByAdminRoot(int parrentID)
        {
            return DB.AdminInstance.uspFunctionGetListByAdminRoot(AdminUtils.ServicesID, parrentID).ToList();
        }

        public List<uspFunctionGetListByAdminIdForLoginResult> GetListFunctionByAdminIDForLogin(int adminID, int serviceID)
        {
            return DB.AdminInstance.uspFunctionGetListByAdminIdForLogin(serviceID, adminID).ToList();
        }

        public List<uspFunctionGetListByAdminRootForLoginResult> GetListFunctionByAdminRootForLogin(int serviceID)
        {
            return DB.AdminInstance.uspFunctionGetListByAdminRootForLogin(serviceID).ToList();
        }

    }
}
